﻿/*$(function () {
    $(document).ajaxSend(function (event, jqXHR, options) {
        var loc = $.url().attr('path');
        if (options.data === undefined) {
            if (options.url.indexOf("?") !== -1) {
                options.url += "&baseUri=" + encodeURIComponent(loc);
            } else {
                options.url += "?baseUri=" + encodeURIComponent(loc);
            }

        } else {
            options.data = "baseUri=" + encodeURI(loc) + "&" + options.data;
        }
    });
});*/

var movic = {
    index: {
        initHomePage: function () {
            //$(".page-content").load(sys.rootPath + "/welcome.jsp");
            movic.common.loadPage(sys.rootPath + "/welcome.html");
            $(".breadcrumb").html('<li><i class="ace-icon fa fa-home home-icon"></i><a href="javascript:movic.index.initHomePage();">首页</a></li><li class="active">欢迎</li>');
        },
        menu: {
            initMenuEvent: function () {
                $("[nav-menu]").each(function () {
                    $(this).bind("click", function () {
                        var lay;
                        var nav = $(this).attr("nav-menu");
                        var sn = nav.split(",");
                        //清除用户信息菜单样式
                        $(".user-menu").find('li').each(function () {
                            $(this).removeClass('active');
                        });
                        //处理监控-新窗口打开
                        if (sn[sn.length - 1] == '/sirona' || sn[sn.length - 1] == '/druid') {
                            window.open(sys.rootPath + sn[sn.length - 1]);
                        } else {
                            var breadcrumb = '<li><i class="ace-icon fa fa-home home-icon"></i><a href="javascript:movic.index.initHomePage();">首页</a></li>';
                            for (var i = 0; i < sn.length - 1; i++) {
                                breadcrumb += '<li class="active">' + sn[i] + '</li>';
                            }
                            lay = layer.load();
                            //设置面包屑内容
                            $(".breadcrumb").html(breadcrumb);
                            //加载页面
                            $(".page-content").empty();//清除该节点子元素
                            $(".page-content").load(sys.rootPath + sn[sn.length - 1], function (data, statusTxt, xhr) {
                                layer.close(lay);
                                if (statusTxt == "error" || statusTxt == "timeout" || statusTxt == "parsererror") {
                                    layer.msg("请求服务器超时,请重新登录或刷新浏览器再试!", {icon: 0});
                                } else {//statusTxt=="success" || statusTxt=="notmodified"

                                    //匹配json字符串
                                    if (data.match("^\{(.+:.+,*){1,}\}$")) {
                                        $(".page-content").empty();//清除该节点子元素

                                        data = $.parseJSON(data);
                                        if (data.status === "401") {
                                            layer.msg(data.message, {icon: 0});
                                            //$(".page-content").load(sys.rootPath + data.url);
                                        } else if (data.status === "403") {
                                            layer.confirm(data.message, {
                                                icon: 3,
                                                title: '提示',
                                                btn: ['重新登录', '取消'] //按钮
                                            }, function (index, layero) {
                                                window.location.href = sys.rootPath + data.url;
                                            });
                                        }
                                    }
                                }
                            });
                        }
                        var level = $(this).parent("li").attr("level");
                        if (level == 'level1' || level == 'level2') {
                            //处理目录类型的点击事件-两级菜单
                            $(this).parent("li").siblings().find("ul.nav-show").removeClass('nav-show').addClass('nav-hide').attr('style', 'display:none');
                            //处理菜单类型的点击事件
                            $(this).parent().parent().parent("li").siblings().find("ul.nav-show").removeClass('nav-show').addClass('nav-hide').attr('style', 'display:none');
                            $("ul.nav-list").find("li.active").removeClass("active").removeClass('open');
                            $(this).parent().addClass("active").parent().parent("li").addClass('active open');
                        } else if (level == 'level3') {
                            //处理目录类型的点击事件-三级菜单
                            $(this).parent("li").siblings().find("ul.nav-show").removeClass('nav-show').addClass('nav-hide').attr('style', 'display:none');
                            //处理菜单类型的点击事件
                            $(this).parent().parent().parent().parent().parent("li").siblings().find("ul.nav-show").removeClass('nav-show').addClass('nav-hide').attr('style', 'display:none');
                            $("ul.nav-list").find("li.active").removeClass("active").removeClass('open');
                            $(this).parent().addClass("active").parent().parent().parent().parent("li").addClass('active open');
                        } else {
                            //处理目录类型的点击事件-四级菜单
                            $(this).parent("li").siblings().find("ul.nav-show").removeClass('nav-show').addClass('nav-hide').attr('style', 'display:none');
                            //处理菜单类型的点击事件
                            $(this).parent().parent().parent().parent().parent().parent().parent("li").siblings().find("ul.nav-show").removeClass('nav-show').addClass('nav-hide').attr('style', 'display:none');
                            $("ul.nav-list").find("li.active").removeClass("active").removeClass('open');
                            $(this).parent().addClass("active").parent().parent().parent().parent().parent().parent("li").addClass('active open');
                        }

                    });
                });
            },
            initDropdownMenuStyle: function () {
                $(".user-menu").find('li').each(function () {
                    $(this).bind('click', function () {
                        $(this).siblings().removeClass('active');
                        $(this).addClass('active');
                    });
                });
            }
        },
        initNavigation: function () {
            $("#ace-settings-navbar").click();
            $("#ace-settings-sidebar").click();
            //$("#ace-settings-breadcrumbs").click();
        },
        initScrollBar: function () {
            $("html").niceScroll({
                cursorborderradius: "5px",
                cursorwidth: 10
            });
        },
        /*
         * 监听浏览器窗口大小变化
         */
        resizeScrollBar: function () {
            $(window).resize(function () {
                $("html").getNiceScroll().resize();
            });
        }
    },
    common: {
        /**
         *加载非菜单展示页面
         * @param nav 待加载的资源URL
         */
        loadPage: function (nav) {
            //加载页面
            $(".page-content").load(sys.rootPath + nav, function (data, statusTxt, xhr) {
                if (statusTxt === "error" || statusTxt === "timeout" || statusTxt === "parsererror") {
                    layer.msg("请求服务器超时,请重新登录或刷新浏览器再试!", {icon: 0});

                } else {
                    if (data.match("^\{(.+:.+,*){1,}\}$")) {
                        $(".page-content").empty();//清除该节点子元素
                        data = $.parseJSON(data);
                        if (data.status === "401") {
                            layer.msg(data.message, {icon: 0});
                            //$(".page-content").load(sys.rootPath + data.url);

                        } else if (data.status === "403") {
                            layer.confirm(data.message, {
                                icon: 3,
                                title: '提示',
                                btn: ['重新登录', '取消'] //按钮
                            }, function (index, layero) {
                                window.location.href = sys.rootPath + data.url;
                            });
                        }
                    }
                }
            });
        },
        /**
         * 新增
         * @param {Object} nav  提交url
         */
        addModel: function (nav) {
            //加载新增页面
            movic.common.loadPage(nav);
        },

        /**
         * 编辑
         * @param {Object} nav  提交url
         */
        editModel: function (nav) {
            //当前页码
            var nowPage = grid.pager.nowPage;
            //获取每页显示的记录数(即: select框中的10,20,30)
            var pageSize = grid.pager.pageSize;
            //获取排序字段
            var columnId = grid.sortParameter.columnId;
            //获取排序方式 [0-不排序，1-正序，2-倒序]
            var sortType = grid.sortParameter.sortType;
            //获取选择的行
            var rows = grid.getCheckedRecords();
            if (rows.length === 1) {
                if (nav === '/user/editUI.html') {
                    if (rows[0].role.key === 'administrator') {
                        layer.msg('该用户为超级管理员,不能编辑!', {
                            icon: 0
                        });
                        return false;
                    }
                }
                if (nav === '/role/editUI.html') {
                    if (rows[0].key === 'administrator') {
                        layer.msg('该角色为基础角色,不能编辑!', {
                            icon: 0
                        });
                        return false;
                    }
                }
                movic.common.loadPage(nav + '?id=' + rows[0].id + "&page=" + nowPage + "&rows=" + pageSize + "&sidx=" + columnId + "&sord=" + sortType);
            } else {
                layer.msg("你没有选择行或选择了多行数据", {
                    icon: 0
                });
            }
        },
        /**
         * 删除
         * @param {Object} nav  提交url
         * @param callback 主函数执行完毕后调用的回调函数名称
         */
        delModel: function (nav, callback) {
            var rows = grid.getCheckedRecords();
            if (rows.length === 1) {
                if (nav === '/user/deleteBatch.html') {
                    if (rows[0].role.key === 'administrator') {
                        layer.msg('该用户为超级管理员,不能删除!', {
                            icon: 0
                        });
                        return false;
                    }
                }
                if (nav === '/role/deleteBatch.html') {
                    if (rows[0].key === 'administrator') {
                        layer.msg('该角色为基础角色,不能删除!', {
                            icon: 0
                        });
                        return false;
                    }
                }
                layer.confirm('确认删除吗？', {
                    icon: 3,
                    title: '删除提示'
                }, function (index, layero) {
                    var delete_ids = [];
                    /*
                     $.each(rows, function(index, value) {
                     ids.push(this.id);
                     });
                     */
                    delete_ids.push(rows[0].id);
                    $.ajax({
                        type: "POST",
                        url: sys.rootPath + nav,
                        data: {
                            "ids": delete_ids.join(',')
                        },
                        dataType: "json",
                        success: function (result) {
                            if (result.success === 200) {
                                layer.msg(result.message, {
                                    icon: 1
                                });
                                if (callback) {
                                    callback();
                                }
                            } else {
                                layer.msg(result.message, {
                                    icon: 5
                                });
                            }
                        },
                        error: function () {
                            layer.msg('服务器未响应,请稍后再试', {
                                icon: 3
                            });
                        }
                    });
                    layer.close(index);
                });
            } else {
                layer.msg("你没有选择行或选择了多行数据", {
                    icon: 0
                });
            }
        },
        /**
         * 提交表单
         * 适用场景：form表单的提交，主要用在用户、角色、资源等表单的添加、修改等
         * @param {Object} commitUrl 表单提交地址
         * @param {Object} listUrl 表单提交成功后转向的列表页地址
         */
         commit: function (formId, commitUrl, jumpUrl) {
            //组装表单数据
            var index;
            var data = $("#" + formId).serialize();
            if (undefined !== $("#pageNum").val()) {
                jumpUrl = jumpUrl + '?page=' + $("#pageNum").val() + '&rows=' + $("#pageSize").val() + '&sidx=' + $("#orderByColumn").val() + '&sord=' + $("#orderByType").val();
            }
            console.log(data);
            $.ajax({
                type: "POST",
                url: sys.rootPath + commitUrl,
                data: data,
                dataType: "json",
                beforeSend: function () {
                    index = layer.load();
                },
                success: function (result) {
                    layer.close(index);
                    if (result.code === 200) {
                        layer.msg(result.message, {
                            icon: 1
                        });
                        movic.common.loadPage(jumpUrl);
                    } else {
                        layer.msg(result.message, {
                            icon: 5
                        });
                    }
                },
                error: function (data) {
                    layer.close(index);
                    layer.msg(data.responseText, {
                        icon: 2
                    });
                }
            });
        }
    },
    form: {
        schedule: {
            editModel: function (nav) {
                //获取选择的行
                var rows = grid.getCheckedRecords();
                if (rows.length == 1) {
                    movic.common.loadPage(nav + '?jobName=' + rows[0].jobName + '&jobGroup=' + rows[0].jobGroup + '&triggerName=' + rows[0].triggerName + '&triggerGroup=' + rows[0].triggerGroup);
                } else {
                    layer.msg("你没有选择行或选择了多行数据", {
                        icon: 0
                    });
                }
            },

            delModel: function (nav, callback) {
                var index;
                var rows = grid.getCheckedRecords();
                if (rows.length == 1) {
                    var jobName = $("#jobName").val();
                    var param = {};
                    if (undefined == jobName) {
                        //执行job
                        param.jobName = rows[0].jobName;
                        param.jobGroup = rows[0].jobGroup;
                    } else {
                        //执行trigger
                        param.triggerName = rows[0].triggerName;
                        param.triggerGroup = rows[0].triggerGroup;
                    }

                    layer.confirm('确认删除吗？', {
                        icon: 3,
                        title: '删除提示'
                    }, function (index, layero) {
                        $.ajax({
                            type: "POST",
                            url: sys.rootPath + nav,
                            data: param,
                            dataType: "json",
                            success: function (resultdata) {
                                if (resultdata.success) {
                                    layer.msg(resultdata.message, {
                                        icon: 1
                                    });
                                    if (callback) {
                                        callback();
                                    }
                                } else {
                                    layer.msg(resultdata.message, {
                                        icon: 5
                                    });
                                }
                            },
                            error: function (errorMsg) {
                                layer.msg('服务器未响应,请稍后再试', {
                                    icon: 3
                                });
                            }
                        });
                        layer.close(index);
                    });
                } else {
                    layer.msg("你没有选择行或选择了多行数据", {
                        icon: 0
                    });
                }
            },

            executeJob: function (nav, callback) {
                var rows = grid.getCheckedRecords();
                var index;
                if (rows.length == 1) {
                    var jobName = $("#jobName").val();
                    var param = {};
                    if (undefined == jobName) {
                        //执行job
                        param.jobName = rows[0].jobName;
                        param.jobGroup = rows[0].jobGroup;
                    } else {
                        //执行trigger
                        param.triggerName = rows[0].triggerName;
                        param.triggerGroup = rows[0].triggerGroup;
                    }

                    $.ajax({
                        type: "POST",
                        url: sys.rootPath + nav,
                        beforeSend: function () {
                            index = layer.load();
                        },
                        data: param,
                        dataType: "json",
                        success: function (result) {
                            layer.close(index);
                            if (result.code === 200) {
                                layer.msg(result.message, {
                                    icon: 1
                                });
                                if (callback) {
                                    callback();
                                }
                            } else {
                                layer.msg(result.message, {
                                    icon: 5
                                });
                            }
                        },
                        error: function (errorMsg) {
                            layer.msg('服务器未响应,请稍后再试', {
                                icon: 3
                            });
                        }
                    });

                } else {
                    layer.msg("你没有选择任务或选择了多个任务", {
                        icon: 0
                    });
                }
            },

            getTrigger: function (jobName, jobGroup) {
                movic.common.loadPage('/scheduleJob/jobTriggerListUI.html?jobName=' + jobName + '&jobGroup=' + jobGroup);
            }
        }
    }
};